﻿using SMS.cntr;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS.util;
using System.Resources;

namespace SMS.view
{
    public partial class TranscriptForm : Form
    {
        public TranscriptForm()
        {
            InitializeComponent();
        }

        private void GradeOfCourse_Load(object sender, EventArgs e)
        {
            bindBlock();
            bindClass();
            bindCourse();
            bindSemester();
            changeLang(I18nUtil.vnLang);
        }

        private void bindBlock()
        {
            cbBlock.DataSource = GradeCntr.Instance.listBlock();
            cbBlock.DisplayMember = "blo_name";
            cbBlock.ValueMember = "blo_id";
            cbBlock.Text = "Chọn Khối";
        }

        private void bindClass()
        {
            cbClass.DataSource = GradeCntr.Instance.findClazz(cbBlock.SelectedValue.ToString());
            cbClass.DisplayMember = "cla_name";
            cbClass.ValueMember = "cla_id";
            cbClass.Text = "Chọn Lớp";
            cbClass.Enabled = false;
            
        }

        private void bindCourse()
        {
            cbCourse.DataSource = GradeCntr.Instance.listCourse();
            cbCourse.DisplayMember = "cor_name";
            cbCourse.ValueMember = "cor_id";
            cbCourse.Text = "Chọn Môn Học";
            cbCourse.Enabled = false;
        }

        private void bindSemester()
        {
            cbSemester.DataSource = GradeCntr.Instance.listSemester();
            cbSemester.DisplayMember = "sem_name";
            cbSemester.ValueMember = "sem_id";
            cbSemester.Text = "Chọn Học Kỳ";
            cbSemester.Enabled = false;
        }

        private void bindGridViewListAll()
        {
            gridViewGrade.Rows.Clear();
            int claID = int.Parse(cbClass.SelectedValue.ToString());
            int corID = int.Parse(cbCourse.SelectedValue.ToString());
            int semID = int.Parse(cbSemester.SelectedValue.ToString());
            DataTable table = GradeCntr.Instance.listGradeAll(claID, corID, semID);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                gridViewGrade.Rows.Add(i + 1, table.Rows[i]["STU_FULLNAME"].ToString(), table.Rows[i]["stu_dob"].ToString(), table.Rows[i]["GRA_FIFTEENMINTEST"].ToString(), table.Rows[i]["GRA_SIXTYMINTEST"].ToString(), table.Rows[i]["GRA_FINALTEST"].ToString());
                gridViewGrade.Rows[gridViewGrade.RowCount - 1].Tag = table.Rows[i];
            }
            gridViewGrade.ClearSelection();
        }

        private void bindGridViewListByClass()
        {
            gridViewGrade.Rows.Clear();
            int claID = int.Parse(cbClass.SelectedValue.ToString());
            DataTable table = GradeCntr.Instance.listGradeByClass(claID);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                gridViewGrade.Rows.Add(i + 1, table.Rows[i]["STU_FULLNAME"].ToString(), table.Rows[i]["stu_dob"].ToString(), table.Rows[i]["GRA_FIFTEENMINTEST"].ToString(), table.Rows[i]["GRA_SIXTYMINTEST"].ToString(), table.Rows[i]["GRA_FINALTEST"].ToString());
                gridViewGrade.Rows[gridViewGrade.RowCount - 1].Tag = table.Rows[i];
            }
            gridViewGrade.ClearSelection();
        }

        private void bindGridViewListByCourse()
        {
            gridViewGrade.Rows.Clear();
            int claID = int.Parse(cbClass.SelectedValue.ToString());
            int corID = int.Parse(cbCourse.SelectedValue.ToString());
            DataTable table = GradeCntr.Instance.listGradeByCourse(claID, corID);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                gridViewGrade.Rows.Add(i + 1, table.Rows[i]["STU_FULLNAME"].ToString(), table.Rows[i]["stu_dob"].ToString(), table.Rows[i]["GRA_FIFTEENMINTEST"].ToString(), table.Rows[i]["GRA_SIXTYMINTEST"].ToString(), table.Rows[i]["GRA_FINALTEST"].ToString());
                gridViewGrade.Rows[gridViewGrade.RowCount - 1].Tag = table.Rows[i];
            }
            gridViewGrade.ClearSelection();
        }

        private void cbClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                bindGridViewListByClass();
                cbCourse.Enabled = true;
                cbSemester.Enabled = false;
            }
            catch (Exception ex) { }
        }

        private void cbCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                bindGridViewListByCourse();
                cbSemester.Enabled = true;
            }
            catch (Exception ex) { }
        }

        private void cbSemester_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                bindGridViewListAll();
            }
            catch (Exception ex) { }
        }

        private void cbBlock_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                bindClass();
                cbClass.Enabled = true;
                cbCourse.Enabled = false;
                cbSemester.Enabled = false;
            }
            catch (Exception ex) { }
        }

        public void changeLang(string lang)
        {
            I18nUtil.init(lang, new ResourceManager(I18nUtil.resourcePath, typeof(TranscriptForm).Assembly));

            this.Text = I18nUtil.getString("transcript");
            label1.Text = I18nUtil.getString("lblClass");
            label2.Text = I18nUtil.getString("lblCourse");
            label3.Text = I18nUtil.getString("lblSemester");
            label4.Text = I18nUtil.getString("lblBlock");
            groupBox1.Text = I18nUtil.getString("gbGradeList");
            groupBox2.Text = I18nUtil.getString("gbClassInfo");
            groupBox2.Text = I18nUtil.getString("gbCourseInfo");
            gridViewGrade.Columns[0].HeaderText = I18nUtil.getString("no");
            gridViewGrade.Columns[1].HeaderText = I18nUtil.getString("fullName");
            gridViewGrade.Columns[2].HeaderText = I18nUtil.getString("DTB");
            gridViewGrade.Columns[3].HeaderText = I18nUtil.getString("fifteenMinTest");
            gridViewGrade.Columns[4].HeaderText = I18nUtil.getString("sixtyMinTest");
            gridViewGrade.Columns[5].HeaderText = I18nUtil.getString("finalTest");
        }
    }
}
